package com.space.myapollo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;

public class MybatisCommon {
//	public static PageInterceptor getPageHelperMySql() {
//		// mybatis分页
//		PageInterceptor pageHelper = new PageInterceptor();
//		Properties props = new Properties();
//		// 但是 sqlserver2012 的时候需要指定, 5.0+ 改用helperDialect，这里让它自动识别数据库类型就好了
//		props.setProperty("helperDialect", "mysql");
//		props.setProperty("reasonable", "true");
//		props.setProperty("supportMethodsArguments", "true");
//		props.setProperty("returnPageInfo", "check");
//		props.setProperty("params", "count=countSql");
//		// 添加插件
//		pageHelper.setProperties(props);
//		return pageHelper;
//	}
//
//	public static PageInterceptor getPageHelper() {
//		// mybatis分页
//		PageInterceptor pageHelper = new PageInterceptor();
//		Properties props = new Properties();
//		// 但是 sqlserver2012 的时候需要指定, 5.0+ 改用helperDialect，这里让它自动识别数据库类型就好了
//		props.setProperty("helperDialect", "sqlserver");
//		props.setProperty("reasonable", "true");
//		props.setProperty("supportMethodsArguments", "true");
//		props.setProperty("returnPageInfo", "check");
//		props.setProperty("params", "count=countSql");
//		// 添加插件
//		pageHelper.setProperties(props);
//		return pageHelper;
//	}

	public static List<String> getPackages(String typeAliasesPackage) {
		// 设置typeAliases 扫描包,多个用逗号隔开，支持*扫描
		List<String> typeAliasesList = new ArrayList<>();
		for (String item : typeAliasesPackage.split(",")) {
			typeAliasesList.add(getTypeAliasesPackages(item.trim()));
		}
		return typeAliasesList;
	}

	/**
	 * *包 获取完整包
	 * 
	 * @param typeAliasesPackage
	 * @return
	 */
	public static String getTypeAliasesPackages(String typeAliasesPackage) {
		ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
		MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
		typeAliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
				+ ClassUtils.convertClassNameToResourcePath(typeAliasesPackage) + "/**/*.class";
		try {
			List<String> result = new ArrayList<String>();
			Resource[] resources = resolver.getResources(typeAliasesPackage);
			if (resources != null && resources.length > 0) {
				MetadataReader metadataReader = null;
				for (Resource resource : resources) {
					if (resource.isReadable()) {
						metadataReader = metadataReaderFactory.getMetadataReader(resource);
						try {
							result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage()
									.getName());
						} catch (ClassNotFoundException e) {
							throw new RuntimeException(e);
						}
					}
				}
			}
			if (result.size() > 0) {
				HashSet<String> h = new HashSet<String>(result);
				result.clear();
				result.addAll(h);
				typeAliasesPackage = String.join(",", (String[]) result.toArray(new String[0]));

			} else {
				throw new RuntimeException(
						"mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return typeAliasesPackage;
	}
}
